/** SystemUser */
package domain.user;

import java.io.Serializable;
import java.util.logging.Logger;

import json.JSONSerializable;

import org.json.JSONObject;

/** 
 * user but more contianing more json information
 * @author jessienewman
 *  @version 1.0
 */
public class SystemUser implements User, JSONSerializable, Serializable {
	
	/**
	 * Field userId.
	 */
	private int userId;
	
	/**
	 * Field userName.
	 */
	private String userName;
	
	/** Field isActive. */
	private boolean isActive;
	
	/** Field password.	 */
	private String password;
		
	/** Field nameFirst. */
	private String nameFirst;
	
	/** Field nameLast. */
	private String nameLast;
	
	/** Field nameMiddle. */
	private String nameMiddle;
	
	/** Field LOGGER. */
	private static final Logger LOGGER = Logger.getLogger(SystemUser.class.getName());
	
	/** 
	 * creates a system user with no data
	 */
	public SystemUser() {
		userId = 0;
		userName = "";
		isActive = true;
		password = "";
		nameFirst = "";
		nameLast = "";
		nameMiddle = "";
	}
	
	/**
	 * dretes a system user with a name
	 * @param nameFirst first name
	 * @param nameLast last name
	 * @param nameMiddle middle name
	 */
	public SystemUser(final String nameFirst, final String nameLast, 
			final String nameMiddle) {
		this.isActive = true;
		this.nameFirst = nameFirst;
		this.nameLast = nameLast;
		this.nameMiddle = nameMiddle;
	}
	
	/**
	 * creates a system user
	 * @param userId id for DATABASE
	 * @param userName username for login
	 * @param isActive is not locked out of logging in
	 * @param password password
	 * @param nameFirst first name
	 * @param nameLast last name
	 * @param nameMiddle middle name
	 */
	public SystemUser(final int userId, final String userName,  // $codepro.audit.disable largeNumberOfParameters
					  final boolean isActive, final String password,
					  final String nameFirst, final String nameLast, 
					  final String nameMiddle) {
		this.userId = userId;
		this.userName = userName;
		this.isActive = isActive;
		this.password = password;
		this.nameFirst = nameFirst;
		this.nameLast = nameLast;
		this.nameMiddle = nameMiddle;
	}
	
	/** 
	 * creates a system user from a jsonobject
	 * @param json jsonobject to create user from
	 */
	public SystemUser(final JSONObject json) {
		populateFromJSON(json);
	}

	/**
	 * Method getUserId.	
	 * @return int * 
	 * @see domain.user.User#getUserId() 
	 * */
	@Override
	public int getUserId() {
		return userId;
	}

	/**
	 * Method setUserId.
	 * @param userId int
	
	 * @see domain.user.User#setUserId(int) */
	@Override
	public void setUserId(final int userId) {
		this.userId = userId;
	}

	/**
	 * Method getUsername.
	 * @return String * 
	 * @see domain.user.User#getUsername() */
	@Override
	public String getUsername() {
		return userName;
	}

	/**
	 * Method setUsername.
	 * @param userName String
	
	 * @see domain.user.User#setUsername(String) */
	@Override
	public void setUsername(final String userName) {
		this.userName = userName;
	}

	/**
	 * Method isActive.
	 * @return boolean * 
	 * @see domain.user.User#isActive() */
	@Override
	public boolean isActive() {
		return isActive;
	}

	/**
	 * Method setActive.
	 * @param isActive boolean
	
	 * @see domain.user.User#setActive(boolean) */
	@Override
	public void setActive(final boolean isActive) {
		this.isActive = isActive;
	}

	/**
	 * Method getPassword.
	 * @return String * 
	 * @see domain.user.User#getPassword() */
	@Override
	public String getPassword() {
		return password;
	}

	/**
	 * Method setPassword.
	 * @param password String
	
	 * @see domain.user.User#setPassword(String) */
	@Override
	public void setPassword(final String password) {
		this.password = password;
	}
	
	/**
	 * Method getNameFirst.
	
	 * @return String */
	public String getNameFirst() {
		return nameFirst;
	}
	
	/**
	 * Method setNameFirst.
	 * @param nameFirst String
	 */
	public void setNameFirst(final String nameFirst) {
		this.nameFirst = nameFirst;
	}
	
	/**
	 * Method getNameLast.
	
	 * @return String */
	public String getNameLast() {
		return nameLast;
	}
	
	/**
	 * Method setNameLast.
	 * @param nameLast String
	 */
	public void setNameLast(final String nameLast) {
		this.nameLast = nameLast;
	}
	
	/**
	 * Method getNameMiddle.
	
	 * @return String */
	public String getNameMiddle() {
		return nameMiddle;
	}
	
	/**
	 * Method setNameMiddle.
	 * @param nameMiddle String
	 */
	public void setNameMiddle(final String nameMiddle) {
		this.nameMiddle = nameMiddle;
	}
	
	/**
	 * Method toString.
	 * @return String * 
	 * @see json.JSONSerializable#toString() */
	@Override
	public String toString() {
		return getJSON().toString();
	}
	
	/**
	 * gets the full name in the form "Last, First M
	
	 * @return full name */
	public String getFullName() {
		return getNameLast() + ", " + getNameFirst() + " " + 
		(!getNameMiddle().equals("") ? getNameMiddle().substring(0, 1) : "");

	}

	/**
	 * Method getJSON.
	 * @return JSONObject * 
	 * @see json.JSONSerializable#getJSON() */
	@Override
	public JSONObject getJSON() {
		final JSONObject json = new JSONObject();
		json.put("userId", userId);
		json.put("userName", userName);
		json.put("isActive", isActive);
		json.put("password", password);
		json.put("nameFirst", nameFirst);
		json.put("nameLast", nameLast);
		json.put("nameMiddle", nameMiddle);
		return json;
	}
	
	/**
	 * Method populateFromJSON.
	 * @param json JSONObject
	
	 * @see json.JSONSerializable#populateFromJSON(JSONObject) */
	@Override
	public void populateFromJSON(final JSONObject json) {
		try {
			setUserId(json.getInt("userId"));
		} catch (NumberFormatException nfe) {
			LOGGER.warning("Number Format Exception: " + nfe);
		}
		setUsername(json.getString("userName"));
		setActive(json.getBoolean("isActive"));
		setPassword(json.getString("password"));
		setNameFirst(json.getString("nameFirst"));
		setNameLast(json.getString("nameLast"));
		setNameMiddle(json.getString("nameMiddle"));
	}

}
